package com.hbt.springbootquick.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hbt.springbootquick.dao.HyUserMapper;
import com.hbt.springbootquick.entity.HyUser;
import com.hbt.springbootquick.service.MyService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Controller
public class MyController {
    @Autowired
    HyUserMapper hyUserMapper;
    @Autowired
    MyService myService;

    @RequestMapping("gt")
    public String asdsad(){
        return "1";
    }

    @RequestMapping("/getUserResult")
    @ResponseBody
    public String as5d4(@RequestParam("userid") String id){
        System.out.println("huya userid:"+id);
        HyUser user = hyUserMapper.getHyUserById(id);
        return String.valueOf(user.getGrade());
    }

    @RequestMapping("/sendYz")
    @ResponseBody
    public String asdaasd(@RequestParam("json") String hyUser_json, HttpSession session){
        System.out.println(hyUser_json);
        JSONObject object = JSONObject.fromString(hyUser_json);
        List<String> result = (List<String>) session.getServletContext().getAttribute("result");
        myService.insertData(hyUser_json, result);
        return "acsa";
    }


    @RequestMapping("/getOrders/{count}")
    @ResponseBody
    public String getOrders(@PathVariable("count") int count){
        List<HyUser> hyUsers = hyUserMapper.getHyUserByCount(count);

//        JSONObject array = JSONObject.fromBean(hyUsers);
        JSONArray array = JSONArray.fromCollection(hyUsers);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "卡水电费水电费");
        jsonObject.put("users", array);
        return jsonObject.toString();
    }

    @RequestMapping("/setResult/{count}")
    @ResponseBody
    public String asfsdf(@PathVariable("count") int count, HttpSession session){
        //删除hyuser中userlevel不是100的数据
        hyUserMapper.deleteUsersByCondition(100);
        System.out.println("delete all users");
        //押注选项
        String[] strs = new String[]{"螃蟹", "鱼儿", "虾米", "铜钱", "公鸡", "葫芦"};

        //自动生成结果
        int touzi_num = count/2;
        List<String> results = new LinkedList<>();
        for (int i = 0; i < touzi_num; i++){
            int radint = new Random().nextInt(count);
            results.add(strs[radint]);
        }
        session.getServletContext().setAttribute("result", results);
        JSONObject object = new JSONObject();
        object.put("result", JSONArray.fromCollection(results));
        return object.toString();
    }


}
